package com.theme.passport.controller.sys;

import com.theme.common.base.StringHandler;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.SysDocumentEntity;
import com.theme.passport.entity.constant.DocumentStatusEnum;
import com.theme.passport.entity.constant.DocumentTypeEnum;
import com.theme.passport.service.SysDocumentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 文档管理
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-12-08 15:14:01
 */
@RestController
@RequestMapping("sys/document")
public class SysDocumentController {
    @Autowired
    private SysDocumentService sysDocumentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:document:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDocumentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:document:info")
    public R info(@PathVariable("id") Integer id){
		SysDocumentEntity sysDocument = sysDocumentService.getById(id);

        return R.ok().put("document", sysDocument);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:document:save")
    public R save(@RequestBody SysDocumentEntity sysDocument){
        sysDocument.setCreateTime(new Date());
        sysDocument.setUpdateTime(new Date());
        sysDocument.setState(DocumentStatusEnum.NOT_PUBLISH.getValue());
        DocumentTypeEnum documentTypeEnum = DocumentTypeEnum.getDocumentTypeEnum(sysDocument.getTypeId());
        sysDocument.setTypeName(documentTypeEnum.getName());
        sysDocumentService.save(sysDocument);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:document:update")
    public R update(@RequestBody SysDocumentEntity sysDocument){
        Integer typeId = sysDocument.getTypeId();
        if (StringHandler.isEmpty(typeId)){
            SysDocumentEntity update = sysDocumentService.getById(sysDocument.getId());
            sysDocument.setTypeId(update.getTypeId());
        }
        if (sysDocument.getState() != null){
            sysDocument.setPublishTime(new Date());
        }
        sysDocument.setUpdateTime(new Date());
        DocumentTypeEnum documentTypeEnum = DocumentTypeEnum.getDocumentTypeEnum(sysDocument.getTypeId());
        sysDocument.setTypeName(documentTypeEnum.getName());
		sysDocumentService.updateById(sysDocument);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:document:delete")
    public R delete(@RequestBody Integer[] ids){
		sysDocumentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
